﻿using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace Com.Lancetinc.Evs.Controls
{
    public enum VideoTimeDisplayAction
    {
        None,
        Playing,
        Recoding
    }

    /// <summary>
    /// VideoTimeDisplayUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class VideoTimeDisplayUserControl
    {
        private VideoTimeDisplayAction _action;
        public VideoTimeDisplayAction Action
        {
            get => _action;
            set
            {
                if (_action == value)
                    return;
                _action = value;
                if (_flashStoryboard == null)
                    return;

                if (BorderRecoding.Visibility == Visibility.Visible)
                {
                    _flashStoryboard.Stop(BorderRecoding);
                }

                if (_action == VideoTimeDisplayAction.Recoding)
                {
                    BorderRecoding.Visibility = Visibility.Visible;
                    _flashStoryboard.Begin(BorderRecoding);
                }
                else
                {
                    BorderRecoding.Visibility = Visibility.Collapsed;
                }
            }
        }

        #region ExperBeginTime
        private DateTime? mExperBeginTime;
        public DateTime? ExperBeginTime
        {
            get => mExperBeginTime;
            set
            {
                if (mExperBeginTime != value)
                {
                    mExperBeginTime = value;
                    RenderCurrentTimeText();
                }
            }
        }
        #endregion

        #region VideoMillSeconds
        private long mVideoMillSeconds = 0;
        public long VideoMillSeconds
        {
            get => mVideoMillSeconds;
            set
            {
                if (mVideoMillSeconds != value)
                {
                    mVideoMillSeconds = value;
                    RenderCurrentTimeText();
                }
            }
        }
        #endregion

        private void RenderCurrentTimeText()
        {
            if (!mExperBeginTime.HasValue)
            {
                this.Visibility = Visibility.Hidden;
                LabelCurrentTimeText.Content = "";
            }
            else
            {
                this.Visibility = Visibility.Visible;
                LabelCurrentTimeText.Content = mExperBeginTime.Value.AddMilliseconds(VideoMillSeconds).ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        private readonly Storyboard _flashStoryboard;
        public VideoTimeDisplayUserControl()
        {
            InitializeComponent();
            _flashStoryboard = (Storyboard)this.FindResource("flashAnimation");
            this.Visibility = Visibility.Hidden;
        }
    }
}
